package com.example.coursemanagement.controller;


import com.example.coursemanagement.MyUtils.CommonResult;
import com.example.coursemanagement.MyUtils.ResultCode;
import com.example.coursemanagement.mapper.CommentMapper;
import com.example.coursemanagement.pojo.Comment;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yangmenghen
 * @since 2020-06-01
 */

//没问题
@RestController
@RequestMapping("/comment" )
@RequiredArgsConstructor
public class CommentController{
    private final CommentMapper commentMapper;

    @ApiOperation(value = "根据课程id获取本课程的评论库")
    @RequestMapping(value = "/select",method = RequestMethod.GET)
    public CommonResult<Object> queryCommentById(Comment comment){
        Map<String,Object> map = new HashMap<>();
        map.put("course_id",comment.getCourseId());
        Collection<Comment> comments = commentMapper.selectByMap(map);
        if(comments==null){
            return CommonResult.failed(ResultCode.FAILED);
        }
        else{
            return CommonResult.success(comments);
        }
    }

    @ApiOperation("学生评论功能")
    @RequestMapping(value = "/addComment",method = RequestMethod.POST)
    public CommonResult<Object> addComment(Comment comment){
        if(commentMapper.insert(comment)>0){
            return CommonResult.success(ResultCode.ADD_SUCCESS);
        }
        else{
            return CommonResult.failed(ResultCode.FAILED);
        }
    }

    @ApiOperation("编辑")
    @RequestMapping(value = "/updateComment",method = RequestMethod.POST)
    public CommonResult<Object> updateComment(Comment comment){
        if(commentMapper.updateById(comment)>0){
            return CommonResult.success(ResultCode.UPDATE_SUCCESS);
        }
        else{
            return CommonResult.failed(ResultCode.FAILED);
        }
    }

    @ApiOperation("管理员删除评论")
    @RequestMapping(value = "/deleteComment",method = RequestMethod.DELETE)
    public CommonResult<Object> delete(int id){
        if(commentMapper.deleteById(id)>0){
            return CommonResult.success(ResultCode.DELETE_SUCCESS);
        }
        else{
            return CommonResult.failed(ResultCode.FAILED);
        }
    }

}

